package com.yimixian.app.common;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yimixian.app.R;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.net.HttpApi;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/3 0003.
 */
public class StopNoticeActivity extends Activity {

    LinearLayout ll_refresh, loading_view;
    TextView tv_time1, tv_time2, tv_time3, tv_time4;
    private DataManager mDataManager;
    String etime, ctime;
    Date eDate, cDate;
    SimpleDateFormat sdf;
    Handler handler = new Handler();
    Calendar calendar = Calendar.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stop_notice);

        ll_refresh = (LinearLayout) findViewById(R.id.ll_refresh);
        loading_view = (LinearLayout) findViewById(R.id.loading_view);
        tv_time1 = (TextView) findViewById(R.id.tv_time1);
        tv_time2 = (TextView) findViewById(R.id.tv_time2);
        tv_time3 = (TextView) findViewById(R.id.tv_time3);
        tv_time4 = (TextView) findViewById(R.id.tv_time4);
        mDataManager = DataManager.getInstance();

        etime = getIntent().getStringExtra("etime");
        ctime = getIntent().getStringExtra("ntime");

        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            eDate = sdf.parse(etime);
            cDate = sdf.parse(ctime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        ll_refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                refresh();
            }
        });

        showEndtime();

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(isReachEndTime()){
                    ll_refresh.setVisibility(View.VISIBLE);
                }else{
                    handler.postDelayed(this, 1000);
                }
            }
        }, 1000);
    }

    private void showEndtime(){
        boolean flag = false;
        try {
            Date eDate = sdf.parse(etime);
            int hour = eDate.getHours();
            int minute = eDate.getMinutes();
            String houStr1 = hour < 10 ? "0" :  (hour + "").substring(0, 1);
            String houStr2 = hour < 10 ? hour + "" : (hour + "").substring(1, 2);
            String minuteStr1 = minute < 10 ? "0" :  (minute + "").substring(0, 1);
            String minuteStr2 = minute < 10 ? minute + "" : (minute + "").substring(1, 2);
            tv_time1.setText(houStr1);
            tv_time2.setText(houStr2);
            tv_time3.setText(minuteStr1);
            tv_time4.setText(minuteStr2);
            flag = true;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if(!flag){
            tv_time1.setText("-");
            tv_time2.setText("-");
            tv_time3.setText("-");
            tv_time4.setText("-");
        }
    }

    /**
     * 是否到达维护截止时间
     * @return
     */
    private boolean isReachEndTime(){

        if(eDate != null && cDate != null){
            calendar.setTime(cDate);
            calendar.add(Calendar.SECOND, 1);
            cDate = calendar.getTime();
            return cDate.compareTo(eDate) > 0;
        }

        return false;
    }

    private void refresh(){

        loading_view.setVisibility(View.VISIBLE);

        Map<String, String> map = new HashMap<>();
        map.put("token", mDataManager.<String>get(CacheKeys.TOKEN));

        HttpApi.startGetRequest("/v5/app_config/android", map, new HttpApi.RequestCallBack() {
            @Override
            public void requestSuccess(String result) {
                loading_view.setVisibility(View.INVISIBLE);
                try {
                    JSONObject json = new JSONObject(result);
                    JSONObject app_config = json.getJSONObject("app_config");
                    JSONObject maintenance = app_config.getJSONObject("maintenance");
                    if ("true".equalsIgnoreCase(maintenance.getString("status"))) {
                        etime = maintenance.getString("etime");
                        ctime = maintenance.getString("ntime");
                        showEndtime();
                        if (isReachEndTime()) {
                            ll_refresh.setVisibility(View.VISIBLE);
                        } else {
                            ll_refresh.setVisibility(View.INVISIBLE);
                        }
                    } else {
                        setResult(10010);
                        finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void requestError() {
                loading_view.setVisibility(View.INVISIBLE);
                Toast.makeText(StopNoticeActivity.this, "刷新失败,请稍后再试", Toast.LENGTH_LONG).show();
            }

            @Override
            public void alwaysDo() {
            }
        });
    }


}
